package com.eamois.dao.graph.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.graph.GraphTemplateTypeDao;
import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.domain.graph.GraphJobCard;
import com.eamois.domain.graph.GraphTemplateType;
import com.eamois.domain.graph.ManualWorkCard;

public class GraphTemplateTypeDaoImpl extends BaseDaoImpl<GraphTemplateType> implements GraphTemplateTypeDao{

	@Override
	@SuppressWarnings("unchecked")
	public Collection<GraphTemplateType> findRootGraphTemplateType() {
		// TODO Auto-generated method stub
		return (Collection<GraphTemplateType>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(GraphTemplateType.class);
				criteria.add(Restrictions.isNull("parentType"));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<GraphTemplateType> findGraphTemplateTypeByParent(
			final GraphTemplateType graphTemplateType) {
		// TODO Auto-generated method stub
		return (Collection<GraphTemplateType>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(GraphTemplateType.class);
				criteria.add(Restrictions.eq("parentType", graphTemplateType));
				return criteria.list();
			}
			
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<GraphTemplateType> findAllType() {
		// TODO Auto-generated method stub
		return (Collection<GraphTemplateType>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(GraphTemplateType.class);
				return criteria.list();
			}
			
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<GraphTemplateType> findGraphTemplateTypeByJobCard(
			final GraphJobCard graphJobCard) {
		return (Collection<GraphTemplateType>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(GraphTemplateType.class);
				criteria.add(Restrictions.eq("graphJobCard", graphJobCard));
				return criteria.list();
			}
			
		});
	}


}
